<?php

namespace App\Admin\Controllers;

use App\Models\OrderItem;
use App\Models\Product;
use App\Models\ProductComment;
use App\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class ProductCommnetController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '商品评论';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ProductComment);

        $grid->column('id', __('Id'));
        $grid->column('user_id', __('用户'))->display(function(){
            return User::getMobileById($this->user_id);
        });
        $grid->column('product_id', __('购买商品'))->display(function(){
            $product = Product::find($this->product_id);
            if($product){
                $product = $product->title;
            }
            return $product;
        });
        $grid->column('order_item_id', __('购买规格*数量'))->display(function(){
            $order_item = OrderItem::find($this->order_item_id);
            if ($order_item) {
                return $order_item['sku_name'].'*'.$order_item['amount'];
            }
            return '';
        });
        $grid->column('commnet', __('评论内容'));
        $grid->column('rating', __('评分'))->display(function(){
            return $this->rating.' 星';
        });
        $grid->column('created_at', __('评论时间'));
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ProductComment::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('user_id', __('User id'));
        $show->field('product_id', __('Product id'));
        $show->field('order_item_id', __('Order item id'));
        $show->field('commnet', __('Commnet'));
        $show->field('rating', __('Rating'));
        $show->field('reply', __('Reply'));
        $show->field('reply_at', __('Reply at'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ProductComment);

        $form->textarea('commnet', __('评论内容'));
        $form->number('rating', __('评分'));
        $form->textarea('reply', __('回复内容'));
        $form->datetime('reply_at', __('回复时间'))->format('YYYY-MM-DD HH:mm:ss')->default(date('Y-m-d H:i:s'));
        return $form;
    }
}
